﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;

namespace ESOL.Business.Hr
{
    /// <summary>
    /// MstDepartment
    /// </summary>
    public partial class MstDepartmentBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstDepartmentEntity GetMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.SelectSingle<MstDepartmentEntity>(objMstDepartmentEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Insert(objMstDepartmentEntity);
        }

        public static bool InsertMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Insert(objMstDepartmentEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Update(objMstDepartmentEntity);
        }

        public static bool UpdateMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Update<MstDepartmentEntity>(objMstDepartmentEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Delete(objMstDepartmentEntity);
        }

        public static bool DeleteMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Delete<MstDepartmentEntity>(objMstDepartmentEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstDepartmentAll()
        {
            return DataAccess.SelectAllDataSet<MstDepartmentEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstDepartmentEntity> GetMstDepartmentAllList()
        {
            return DataAccess.SelectAll<MstDepartmentEntity>();
        }
        #endregion


        public static DataTable GetDapartmentByCondition(MstDepartmentEntity condition)
        {
            string sql = @"Select D.*, PD.DEPARTMENT_NAME AS PARENT_NAME, P.NAME_CN AS MANAGER_NAME
                        FROM MST_DEPARTMENT D
                        LEFT JOIN MST_DEPARTMENT PD ON PD.DEPARTMENT_ID = D.PARENT_ID
						LEFT JOIN MST_PERSON P ON P.PERSON_ID = D.MANAGER
                        WHERE 1=1 ";
            if (condition != null)
            {
              
                if (!string.IsNullOrEmpty(condition.ParentId))
                    sql += " AND D.PARENT_ID = '" + condition.ParentId + "' ";
         
                if (!string.IsNullOrEmpty(condition.Manager))
                    sql += " AND D.MANAGER = '" + condition.Manager + "' ";

                if (!string.IsNullOrEmpty(condition.DepartmentCode))
                    sql += " AND D.DEPARTMENT_CODE LIKE '%" + condition.DepartmentCode + "%'";

                if (!string.IsNullOrEmpty(condition.DepartmentName) )
                    sql += " AND D.DEPARTMENT_NAME LIKE '%" + condition.DepartmentName + "%'";
              
                if (condition.DeleteFlag!=null)
                    sql += " AND D.DELETE_FLAG = " + condition.DeleteFlag + "";
               
            }

            sql += " ORDER BY SEQUENCE";
            
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
    }
}
